﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Http;
using System.Web.Http;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Reflection;
using System.Linq.Dynamic;
using Newtonsoft.Json;
using OmixJsonServer.Helpers;
using Omix.Dynamic.Data;
using OmixJsonServer.Results;
using System.Web.Mvc;
using OmixJsonServer.Extensions;

namespace OmixJsonServer.Controllers
{
    [System.Web.Mvc.OutputCache(Duration = 0)]
    public class DbController : ApiController, IDisposable
    {

        public IEnumerable<dynamic> Get(string table)
        {
            try
            {
                var List = DbHelper.DB.Get(table);

                var dynamicList = (List as IEnumerable<dynamic>).ToList();

                return dynamicList;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

        public dynamic Get(string table, string condition,[FromUri] bool single = false)
        {
            try
            {
                var List = DbHelper.DB.Get(table);

                var dynamicList = (List as IEnumerable<dynamic>).AsQueryable().Where(condition).ToList();

                 if (dynamicList != null)
                 {
                     if (single)
                     {
                         return dynamicList.FirstOrDefault();
                     }
                     else
                     {
                         return dynamicList;
                     }
                 }
                 else
                 {
                     return null;
                 }
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

        public void Post([FromUri] string table, [FromBody] dynamic data)
        {
            Type t = DbHelper.DB.ClassBuilder.Types[table];

            dynamic obj = HttpContext.Current.Request.MapToType(t);

            DbHelper.DB.Add(obj);

            DbHelper.DB.Save();

        }

        public void Put([FromUri] string table, [FromBody] dynamic data)
        {
            //Get Type
            Type t = DbHelper.DB.ClassBuilder.Types[table];

            string PKName = DbHelper.DB.Schema.Tables.FirstOrDefault(o => o.Name == table).Columns.FirstOrDefault(p => p.IsPK).ColumnName;

            dynamic obj = HttpContext.Current.Request.MapToType(t);

            int PKValue = ReflectionHelper.GetPropertyValue(obj, PKName);

            dynamic dbSet = ReflectionHelper.GetPropertyValue(DbHelper.DB.Instance, table);

            object dbObject = ((IEnumerable<dynamic>)dbSet).FirstOrDefault(o => o.Id == PKValue);

            dbObject = HttpContext.Current.Request.MapToType(t,dbObject);

            DbHelper.DB.Save();

            //Get PK Name

            //Where to find object by pk value

            //override property values

            //update db
        }

        public void Delete([FromUri] string table, [FromUri] string condition)
        {
        }

    }
}

